package com.kuang.sync.managedown;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kuang.sync.singledown.SingleDownServiceImpl;
import com.kuang.utils.ControllerLogUtils;
import com.kuang.utils.MQTTConnect;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class ManageDownServiceImpl implements ManageDownService{

    @Value("${mqtt.sfurl}")
    String url;

    @Resource
    MQTTConnect mqttConnect;

    Logger logger = Logger.getLogger(ManageDownServiceImpl.class);

    @Override
    public String postManageDeviceRegisterSrv(String id, JSONObject preant) throws MqttException {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String address = "/single/down/manageMachineRegister";

        preant.put("device_code", id);
        String requestBody = JSON.toJSONString(preant);
        String responseBody="";

        try {
            HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.postForEntity(url + address, requestEntity, String.class);
            responseBody = response.getBody();
            // 对响应进行处理
            // 比如转成map
            JSONObject jsonObject = JSONObject.parseObject(responseBody);
            //在这个示例中，我们使用postForEntity()方法发送POST请求，并在请求体中传递了JSON

            JSONObject data = jsonObject.getJSONObject("data");
            boolean pub = mqttConnect.pub("/manage/down/"+id, JSON.toJSONString(data));//发布主题


        }catch (Exception e){
            int i = ControllerLogUtils.setErrorMsgLog("post", "/single/down/manageMachineRegister",
                    "管理机注册",e.getMessage(), JSON.toJSONString(preant), JSON.toJSONString(responseBody));
            logger.info("错误信息:"+e.getMessage());

            //发送返回讯息
            JSONObject toJson = new JSONObject();
            JSONObject data = new JSONObject();
            toJson.put("cmd", MapUtils.getString(preant,"cmd"));
            toJson.put("ver",MapUtils.getString(preant,"ver"));
            data.put("state","0");
            toJson.put("data",data);
            boolean pub = mqttConnect.pub("/manage/down/"+id, JSON.toJSONString(toJson));//发布主题

            return  null;
        }

        logger.info("信息:"+JSON.toJSONString(responseBody));
        int i = ControllerLogUtils.setMsgLog("post", "/single/down/manageMachineRegister",
                "管理机注册","成功", JSON.toJSONString(preant), JSON.toJSONString(responseBody));
        return null;
    }
}
